//
//  MainHeaderView.m
//  Peso Guide
//
//  Created by macbook on 2025/6/7.
//

#import "MainHeaderView.h"
#import "AmountFormatter.h"
#import "ProductDetailViewController.h"
#import "CustomWebViewController.h"
#import "MineSettingViewController.h"

@interface MainHeaderView ()

@property (nonatomic, strong) UIImageView *backImg;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *termContentLabel;
@property (nonatomic, strong) UILabel *rateContentLabel;
@property (nonatomic, strong) UIButton *applyButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *termLabel;
@property (nonatomic, strong) UILabel *rateLabel;

@end

@implementation MainHeaderView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        _backImg = [PesoKit createImageViewWithFrame:CGRectMake(12, 0, KScreenWidth-24, ceil((KScreenWidth-24)*0.96+10)) imageNamed:@"bg_main_header_back_img"];
        _backImg.userInteractionEnabled = YES;
        _backImg.userInteractionEnabled = YES;
        [self addSubview:_backImg];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(applyButtonClick)];
        [_backImg addGestureRecognizer:tapGesture];
        
        _titleLabel = [PesoKit createLabelWithFrame:CGRectMake(0, 100, _backImg.width, 20) backGroundColor:[UIColor clearColor] text:@"" textColor:ColorHex(@"#666666") font:FONT(FontWeightStyleRegular, 14)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [_backImg addSubview:_titleLabel];
        
        UIImageView *centerImg = [PesoKit createImageViewWithFrame:CGRectMake(39, _titleLabel.bottom+10, _backImg.width-39*2, 68) imageNamed:@"ic_main_center_img"];
        centerImg.userInteractionEnabled = YES;
        [_backImg addSubview:centerImg];
        
        _priceLabel = [PesoKit createLabelWithFrame:CGRectMake(0, 0, centerImg.width, centerImg.height) backGroundColor:[UIColor clearColor] text:@"" textColor:ColorHex(@"#FFFFFF") font:[UIFont fontWithName:@"Inter-Bold" size:35]];
        _priceLabel.textAlignment = NSTextAlignmentCenter;
        [centerImg addSubview:_priceLabel];
        
        
        _termLabel = [PesoKit createLabelWithFrame:CGRectMake(0, centerImg.bottom+10, _backImg.width/2.0, 20) backGroundColor:[UIColor clearColor] text:@"" textColor:ColorHex(@"#999999") font:FONT(FontWeightStyleRegular, 14)];
        _termLabel.textAlignment = NSTextAlignmentCenter;
        [_backImg addSubview:_termLabel];
        
        _termContentLabel = [PesoKit createLabelWithFrame:CGRectMake(0, _termLabel.bottom+3, _backImg.width/2.0, 20) backGroundColor:[UIColor clearColor] text:@"-" textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleMedium, 14)];
        _termContentLabel.textAlignment = NSTextAlignmentCenter;
        [_backImg addSubview:_termContentLabel];
        
        _rateLabel = [PesoKit createLabelWithFrame:CGRectMake(_backImg.width/2.0, centerImg.bottom+10, _backImg.width/2.0, 20) backGroundColor:[UIColor clearColor] text:@"" textColor:ColorHex(@"#999999") font:FONT(FontWeightStyleRegular, 14)];
        _rateLabel.textAlignment = NSTextAlignmentCenter;
        [_backImg addSubview:_rateLabel];
        
        _rateContentLabel = [PesoKit createLabelWithFrame:CGRectMake(_backImg.width/2.0, _rateLabel.bottom+3, _backImg.width/2.0, 20) backGroundColor:[UIColor clearColor] text:@"-" textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleMedium, 14)];
        _rateContentLabel.textAlignment = NSTextAlignmentCenter;
        [_backImg addSubview:_rateContentLabel];
        
        _applyButton = [PesoKit createButtonWithFrame:CGRectMake(40, _backImg.height-30-60, _backImg.width-80, 60) backGroundColor:[UIColor clearColor] title:@"" titleColor:ColorHex(@"#000000") font:[UIFont fontWithName:@"Inter-Bold" size:18]];
        [_applyButton setBackgroundImage:[UIImage imageNamed:@"ic_main_apply_now"] forState:0];
        [_applyButton addTarget:self action:@selector(applyButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _applyButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 10, 0);
        [_backImg addSubview:_applyButton];
        
        UIImageView *lineImg = [PesoKit createImageViewWithFrame:CGRectMake(_backImg.width/2.0-1, centerImg.bottom+21, 2, 21) imageNamed:@"ic_main_line"];
        [_backImg addSubview:lineImg];
    }
    return self;
}

- (void)setTopList:(NSMutableArray *)topList {
    _topList = topList;
    
    if (topList.count > 0) {
        NSDictionary *dataDic = [NSDictionary safeDictionaryWithDictionary:[[topList safeObjectAtIndex:0] safeObjectAtIndex:0]];
        NSString *exception = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"exception"]];
        NSString *follows = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"follows"]];
        NSString *basis = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"basis"]];
        NSString *raises = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"raises"]];
        NSString *per = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"per"]];
        NSString *buffer = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"buffer"]];
        NSString *assigning = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"assigning"]];

        _titleLabel.text = exception;
        NSArray *array = [follows componentsSeparatedByString:@" "];
        if (array.count == 2) {
            NSString *price = [array safeObjectAtIndex:1];
            _priceLabel.text = [NSString stringWithFormat:@"%@ %@",[array safeObjectAtIndex:0],[AmountFormatter formatAmount:[NSNumber numberWithFloat:[price floatValue]]]];
        }else {
            _priceLabel.text = follows;
        }
        _termLabel.text = basis;
        _termContentLabel.text = raises;
        _rateLabel.text = per;
        _rateContentLabel.text = buffer;
        [_applyButton setTitle:assigning forState:0];
    }
}

- (void)applyButtonClick {
    DLog(@"apply now");
    
    if ([AppTools isLogin] == NO) {
        [AppTools toLogin];
        return;
    }
    
    kPreventRepeatClickTime(1.5);

    if (_topList.count > 0) {
        NSDictionary *dataDic = [NSDictionary safeDictionaryWithDictionary:[[_topList safeObjectAtIndex:0] safeObjectAtIndex:0]];
        NSString *causes = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"causes"]];
        
        [[AWHttpService service] postWithUrl:@"/trains/synonymous" params:@{@"across":causes} succ:^(id  _Nonnull responseObject) {
            if (SUCCESS) {
                NSDictionary *associating = [NSDictionary safeDictionaryWithDictionary:[responseObject safeObjectForKey:@"associating"]];
                NSString *recent = [NSString stringWithFormat:@"%@",[associating safeObjectForKey:@"recent"]];
                
                if ([recent rangeOfString:@"http://"].location != NSNotFound ||
                    [recent rangeOfString:@"https://"].location != NSNotFound) {
                    //webView
                    CustomWebViewController *webVC = [[CustomWebViewController alloc] init];
                    webVC.linkUrl = recent;
                    [[AppTools getCurrentVC].navigationController pushViewController:webVC animated:YES];
                    return;
                }
                if ([recent rangeOfString:@"pe://so.gui.de/thelike"].location != NSNotFound) {
                    //设置
                    MineSettingViewController *settingVC = [[MineSettingViewController alloc] init];
                    settingVC.hidesBottomBarWhenPushed = YES;
                    [[AppTools getCurrentVC].navigationController pushViewController:settingVC animated:YES];
                    return;
                    return;
                }
                if ([recent rangeOfString:@"pe://so.gui.de/andlove"].location != NSNotFound) {
                    //首页
                    [AppTools getCurrentVC].tabBarController.selectedIndex = 0;
                    return;
                }
                if ([recent rangeOfString:@"pe://so.gui.de/standard"].location != NSNotFound) {
                    //登录
                    [AppTools toLogin];
                    return;
                }
                if ([recent rangeOfString:@"pe://so.gui.de/classes"].location != NSNotFound) {
                    //订单
                    [AppTools getCurrentVC].tabBarController.selectedIndex = 1;
                    return;
                }
                if ([recent rangeOfString:@"pe://so.gui.de/islove"].location != NSNotFound) {
                    //产品详情
                    NSArray *array = [recent componentsSeparatedByString:@"="];
                    NSString *theId = @"";
                    if (array.count == 2) {
                        theId = [NSString stringWithFormat:@"%@",[array safeObjectAtIndex:1]];
                    }else {
                        theId = causes;
                    }
                    ProductDetailViewController *vc = [[ProductDetailViewController alloc] init];
                    vc.productId = theId;
                    vc.hidesBottomBarWhenPushed = YES;
                    [[AppTools getCurrentVC].navigationController pushViewController:vc animated:YES];
                    return;
                }
            }else {
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",[responseObject safeObjectForKey:@"order"]]];
                [SVProgressHUD dismissWithDelay:3.0f];

            }
        } fail:^(NSInteger errorType, id  _Nonnull error) {}];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
